package com.erli.exchange.common.interceptor.db;

import java.sql.Connection;
import java.util.Properties;

import org.apache.ibatis.executor.statement.StatementHandler;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.plugin.Intercepts;
import org.apache.ibatis.plugin.Invocation;
import org.apache.ibatis.plugin.Plugin;
import org.apache.ibatis.plugin.Signature;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

@Component
@Intercepts({
		@Signature(type = StatementHandler.class, method = "prepare", args = { Connection.class, Integer.class }) })
@Slf4j
public class StatementInterceptor implements Interceptor {

	@Override
	public Object intercept(Invocation invocation) throws Throwable {
		//        StatementHandler statementHandler = 
		//                (StatementHandler) PluginUtils.realTarget(invocation.getTarget());
		//        MetaObject metaStatementHandler = SystemMetaObject.forObject(statementHandler);
		//        MappedStatement mappedStatement = 
		//                (MappedStatement) metaStatementHandler.getValue("delegate.mappedStatement");
		//        //只拦截select方法
		//        if (!SqlCommandType.SELECT.equals(mappedStatement.getSqlCommandType())) {
		//            return invocation.proceed();
		//        }
		//        BoundSql boundSql = (BoundSql) metaStatementHandler.getValue("delegate.boundSql");
		//        //获取到sql
		//        String originalSql = boundSql.getSql();
		//        //可以对originalSql进行改写
		//        log.error("拦截器StatementInterceptor:"+originalSql);
		//        metaStatementHandler.setValue("delegate.boundSql.sql", originalSql);
		//        Object parameterObject = boundSql.getParameterObject();
		log.info("拦截器StatementInterceptor:");
		return invocation.proceed();
	}

	@Override
	public Object plugin(Object target) {
		return Plugin.wrap(target, this);
	}

	@Override
	public void setProperties(Properties properties) {

	}
}
